package com.syncomponents.xml;

import java.io.IOException;
import java.io.InputStream;
import java.io.StringReader;
import java.net.URL;

import javax.xml.XMLConstants;
import javax.xml.transform.Source;
import javax.xml.transform.stream.StreamSource;
import javax.xml.validation.Schema;
import javax.xml.validation.SchemaFactory;
import javax.xml.validation.Validator;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.xml.sax.SAXException;


/**
 *  validate xml format
 *  
 * @author sunkey
 * @date Jul 18, 2014
 */
public class XmlValidator {
    
    protected static final Logger logger = LoggerFactory.getLogger(XmlValidator.class);

    /**
     * @param args
     */
    public static void validate(URL schemaURL, String xml) throws SAXException, IOException {

        // 1. Lookup a factory for the W3C XML Schema language
        SchemaFactory factory = SchemaFactory.newInstance("http://www.w3.org/2001/XMLSchema");

        // 2. Compile the schema.
        // Here the schema is loaded from a java.io.File, but you could use
        // a java.net.URL or a javax.xml.transform.Source instead.
        Schema schema = factory.newSchema(schemaURL);

        // 3. Get a validator from the schema.
        Validator validator = schema.newValidator();

        // 4. Parse the document you want to check.
        StringReader xmlReader = new StringReader(xml);
        Source source = new StreamSource(xmlReader);

        // 5. Check the document
        try {
            validator.validate(source);
        } catch (SAXException ex) {
            logger.error(ex.getMessage(), ex);
        }

    }

    /**
     * @param args
     */
    public static void validate(URL schemaURL, InputStream inputStream) throws SAXException, IOException {
        
        // 1. Lookup a factory for the W3C XML Schema language
        SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
        
        // 2. Compile the schema.
        // Here the schema is loaded from a java.io.File, but you could use
        // a java.net.URL or a javax.xml.transform.Source instead.
        Schema schema = factory.newSchema(schemaURL);
        
        // 3. Get a validator from the schema.
        Validator validator = schema.newValidator();
        
        // 4. Parse the document you want to check.
        Source source = new StreamSource(inputStream);
        
        // 5. Check the document
        try {
            validator.validate(source);
        } catch (SAXException ex) {
            logger.error(ex.getMessage(), ex);
        }
        
    }
    

}
//
//class SimpleErrorHandler implements ErrorHandler {
//
//    @Override
//    public void error(SAXParseException arg0) throws SAXException {
//        System.out.printf("error=%s\n", arg0.getMessage());
//    }
//
//    @Override
//    public void fatalError(SAXParseException arg0) throws SAXException {
//        System.out.printf("fatalError=%s\n", arg0.getMessage());
//
//    }
//
//    @Override
//    public void warning(SAXParseException arg0) throws SAXException {
//        System.out.printf("warning=%s\n", arg0.getMessage());
//
//    }
//
//}
